package webday03;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoCookie extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		Date d=new Date();
		
		//时间格式化
		DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss");
		//访问时间
		String date=formatter.format(d);
		
		//取cookie
		//Cookie[] cookies=req.getCookies();
		
		//在服务器写cookie
		Cookie cookie=new Cookie("accessDate",date);
		resp.addCookie(cookie);
		
		// 和response.addCookie效果一样
		// response.addHeader("Set-Cookie", date);
	

	// 让cookie持久化，存到文件里面，浏览器关闭之后，下次启动仍然能够访问

	// 只要过期时间没到，cookie就一直存在文件里面，浏览器关闭也不影响
		cookie.setMaxAge(60);//60秒
		resp.addCookie(cookie);
	}
}
